package dao

import javax.inject.Inject
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile
import models.Tables._

import scala.concurrent.{ExecutionContext, Future}

class EpigDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                       (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insert(row: Seq[EpigeneticsRow]): Future[Unit] = {
    db.run(Epigenetics ++= row).map(_ => ())
  }

  def getByGeneId(geneId: String): Future[Seq[EpigeneticsRow]] = {
    db.run(Epigenetics.filter(_.geneid === geneId).result)
  }

  def getByRange(start: Int, end: Int, epigType: String, chr: String, repeat: String): Future[Seq[EpigeneticsRow]] = {
    db.run(Epigenetics.filter(_.epigType === epigType).filter(_.chrom === chr).filter(_.repeat === repeat).
      filter(x => x.start >= start && x.end <= end).result)
  }

  def getAllGeneId: Future[Seq[String]] = {
    db.run(Epigenetics.map(_.geneid).distinct.result)
  }

  def updateCsPm6AToCsVm :Future[Unit] = {
    db.run(Epigenetics.filter(_.epigType === "CsPm6A").map(_.epigType).update("CsVm6A")).map(_=>())
  }


  def updateCsPm5CToCsVm :Future[Unit] = {
    db.run(Epigenetics.filter(_.epigType === "CsPm5C").map(_.epigType).update("CsVm5C")).map(_=>())
  }

}
